<?php
namespace app\index\controller;

use think\Controller;
use app\admin\model\MessageModel;
use app\admin\model\MemberModel;

/**
 * 留言板
 * @author Alinejun666
 * Date 2018-8-20
 */
class Message extends controller
{
    public function initialize()
    {
        // 验证登录是否过期
        $user = session('member');
        if (!empty($user)) {
            $diff = (int)time() - (int)strtotime($user['active_time']);
            if ($diff > 1800) {
                session("member", null);
            }               
        }
    }

    public function index()
    {
    	
        $message = new MessageModel();
        $member  = new MemberModel();

        $where = ['type' => 1];
        $order = "create_time desc";
        $field = "*";
        $list  = $message->get_list($where, $order, $field);
        foreach ($list as $k => $v) {
            // 会员信息
            $list[$k]['userinfo'] = $member->get_info(['user_id' => $v['user_id']], "user_id, headpic, nickname");
            // 回复
            $where = [
                'type'     => 2,
                'reply_id' => $v['message_id'],
            ];
            $res  = $message->get_count($where);
            if ($res > 0) {
                $order = "create_time desc";
                $reply  = $message->get_list($where, $order, $field="*");
                $list[$k]['reply'] = $reply;
            }
        }

        $user = session("member");
        $data = [
            'list'   => $list,
            'member' => $user,
            'count'  => count($list),
        ];

		return $this->fetch('index', $data);
    }


    public function addmess()
    {

        if (request()->isAjax()) {
            $message = new MessageModel();

            // 登录信息
            $user = session('member');
            if (empty($user)) {
                echo json_encode(msg_error("请先登录.."));exit;
            }else{
		        $diff = (int)time() - (int)strtotime($user['active_time']);
		        if ($diff > 1800) {
		            session("member", null);
                	echo json_encode(msg_error("登录已过期，请重新登录.."));exit;
		        }            	
            }

            // 频率检测
            $where = [
            	'type'    => 1,
            	'user_id' => $user['user_id'],
            ];
            $order = "create_time desc";
            $field = "create_time";
            $info  = $message->get_order_info($where, $order, $field);
	        $diff = (int)time() - (int)strtotime($info['create_time']);
	        if ($diff < 90) {
            	echo json_encode(msg_error("留言频率过快，请歇会儿再来.."));exit;
	        }

            // 入参
            $data  = input("param.");
            $where = [
				'reply_id' => -1,
				'type'     => 1,
				'user_ip'  => get_client_ip(),
				'user_id'  => $user['user_id'],
				'content'  => $data['content'],
				'status'   => $data['status'],
            ];

            // 留言
            $res = $message->insert_data($where);
            if (isset($res['msg'])) {
            	echo json_encode($res);exit;
            }else{
            	echo json_encode(msg_success("留言成功"));exit;
            }

        }else{
            $this->error("发生错误", 'index/index/index');            
        }

    } 

    // 更多
    public function more()
    {
        if (request()->isAjax()) {

            $message = new MessageModel();
            $member  = new MemberModel();

            $page = input("param.page/d");
            $size = input("param.size/d");

            $where = ['type' => 1];
            $order = "create_time desc";
            $field = "*";
            $start = (int)$page*$size - 1;

            // 统计
            $sum = $message->get_count($where);
            if ($start >= ($sum - 1)) {
                echo 3;
                exit;
            }

            $list  = $message->get_list_page($where, $order, $field, $start, $size);
            foreach ($list as $k => $v) {
                // 会员信息
                $list[$k]['userinfo'] = $member->get_info(['user_id' => $v['user_id']], "user_id, headpic, nickname");
                // 回复
                $where = [
                    'type'     => 2,
                    'reply_id' => $v['message_id'],
                ];
                $res  = $message->get_count($where);
                if ($res > 0) {
                    $order = "create_time desc";
                    $reply  = $message->get_list($where, $order, $field="*");
                    $list[$k]['reply'] = $reply;
                }
            }

            echo json_encode($list);
            exit;

        }else{
            $this->error("发生错误", 'index/index/index');            
        }
    }  
}
